﻿using System; 
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Matrix.Common.DBUtility;
using Matrix.MaKaYunShang.Model;

namespace Matrix.MaKaYunShang.DAL
{
	public partial class SmsRecordDAL : BaseDAL<SmsRecordDAL>
	{
        #region 默认方法
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(SmsRecord model)
        {
            var strSql = new StringBuilder();
            strSql.Append("insert into SmsRecord(");
            strSql.Append("WechantInfoId,SendFrom,SendContent,SendCode,SendSMSTime,Mobile,SentResult,SmsSerialNumber,UserActionSource,FailTime,IsDelete,RequestUserId");
            strSql.Append(") values (");
            strSql.Append("@WechantInfoId,@SendFrom,@SendContent,@SendCode,@SendSMSTime,@Mobile,@SentResult,@SmsSerialNumber,@UserActionSource,@FailTime,@IsDelete,@RequestUserId");
            strSql.Append(") ");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
			            new SqlParameter("@WechantInfoId", SqlDbType.Int,4) ,            
                        new SqlParameter("@SendFrom", SqlDbType.Int,4) ,            
                        new SqlParameter("@SendContent", SqlDbType.NVarChar,2560) ,            
                        new SqlParameter("@SendCode", SqlDbType.NVarChar,32) ,            
                        new SqlParameter("@SendSMSTime", SqlDbType.DateTime) ,            
                        new SqlParameter("@Mobile", SqlDbType.NVarChar,32) ,            
                        new SqlParameter("@SentResult", SqlDbType.NVarChar,1024) ,            
                        new SqlParameter("@SmsSerialNumber", SqlDbType.NVarChar,64) ,            
                        new SqlParameter("@UserActionSource", SqlDbType.Int,4) ,            
                        new SqlParameter("@FailTime", SqlDbType.Int,4) ,            
                        new SqlParameter("@IsDelete", SqlDbType.Bit,1) ,            
                        new SqlParameter("@RequestUserId", SqlDbType.Int,4)             
              
            };

            parameters[0].Value = model.WechantInfoId;
            parameters[1].Value = model.SendFrom;
            parameters[2].Value = model.SendContent;
            parameters[3].Value = model.SendCode;
            parameters[4].Value = model.SendSMSTime;
            parameters[5].Value = model.Mobile;
            parameters[6].Value = model.SentResult;
            parameters[7].Value = model.SmsSerialNumber;
            parameters[8].Value = model.UserActionSource;
            parameters[9].Value = model.FailTime;
            parameters[10].Value = model.IsDelete;
            parameters[11].Value = model.RequestUserId; var obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            return obj == null ? 0 : Convert.ToInt32(obj);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(SmsRecord model)
        {
            var strSql = new StringBuilder();
            strSql.Append("update SmsRecord set ");

            strSql.Append(" WechantInfoId = @WechantInfoId , ");
            strSql.Append(" SendFrom = @SendFrom , ");
            strSql.Append(" SendContent = @SendContent , ");
            strSql.Append(" SendCode = @SendCode , ");
            strSql.Append(" SendSMSTime = @SendSMSTime , ");
            strSql.Append(" Mobile = @Mobile , ");
            strSql.Append(" SentResult = @SentResult , ");
            strSql.Append(" SmsSerialNumber = @SmsSerialNumber , ");
            strSql.Append(" UserActionSource = @UserActionSource , ");
            strSql.Append(" FailTime = @FailTime , ");
            strSql.Append(" IsDelete = @IsDelete , ");
            strSql.Append(" RequestUserId = @RequestUserId  ");
            strSql.Append(" where Id=@Id ");

            SqlParameter[] parameters = {
			            new SqlParameter("@Id", SqlDbType.Int,4) ,            
                        new SqlParameter("@WechantInfoId", SqlDbType.Int,4) ,            
                        new SqlParameter("@SendFrom", SqlDbType.Int,4) ,            
                        new SqlParameter("@SendContent", SqlDbType.NVarChar,2560) ,            
                        new SqlParameter("@SendCode", SqlDbType.NVarChar,32) ,            
                        new SqlParameter("@SendSMSTime", SqlDbType.DateTime) ,            
                        new SqlParameter("@Mobile", SqlDbType.NVarChar,32) ,            
                        new SqlParameter("@SentResult", SqlDbType.NVarChar,1024) ,            
                        new SqlParameter("@SmsSerialNumber", SqlDbType.NVarChar,64) ,            
                        new SqlParameter("@UserActionSource", SqlDbType.Int,4) ,            
                        new SqlParameter("@FailTime", SqlDbType.Int,4) ,            
                        new SqlParameter("@IsDelete", SqlDbType.Bit,1) ,            
                        new SqlParameter("@RequestUserId", SqlDbType.Int,4)             
              
            };

            parameters[0].Value = model.Id;
            parameters[1].Value = model.WechantInfoId;
            parameters[2].Value = model.SendFrom;
            parameters[3].Value = model.SendContent;
            parameters[4].Value = model.SendCode;
            parameters[5].Value = model.SendSMSTime;
            parameters[6].Value = model.Mobile;
            parameters[7].Value = model.SentResult;
            parameters[8].Value = model.SmsSerialNumber;
            parameters[9].Value = model.UserActionSource;
            parameters[10].Value = model.FailTime;
            parameters[11].Value = model.IsDelete;
            parameters[12].Value = model.RequestUserId; return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters) > 0;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {
            var strSql = new StringBuilder();
            strSql.Append("update SmsRecord set IsDelete=1 ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters) > 0;
        }

        /// <summary>
        /// 批量删除一批数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            var strSql = new StringBuilder();
            strSql.Append("update SmsRecord set IsDelete=1 ");
            strSql.Append(" where Id in (" + Idlist + ")  ");
            return DbHelperSQL.ExecuteSql(strSql.ToString()) > 0;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DataSet GetModel(int Id)
        {
            var strSql = new StringBuilder();
            strSql.Append("select Id, WechantInfoId, SendFrom, SendContent, SendCode, SendSMSTime, Mobile, SentResult, SmsSerialNumber, UserActionSource, FailTime, IsDelete, RequestUserId  ");
            strSql.Append("  from SmsRecord ");
            strSql.Append(" where Id=@Id  and IsDelete=0");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            var ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            return ds.Tables[0].Rows.Count > 0 ? ds : null;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            var strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM SmsRecord where IsDelete=0 ");
            if (!string.IsNullOrEmpty(strWhere))
            {
                strSql.Append(" and " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
        #endregion
		
		#region 扩展方法
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DataSet GetModelByMobileOrCode(string mobile, string code, int userActionSource)
        {
            var strSql = new StringBuilder();
            strSql.Append("select * from SmsRecord ");
            strSql.Append(" where IsDelete=0 and mobile=@mobile and sendCode=@code and UserActionSource=@userActionSource order by SendSMSTime desc");
            SqlParameter[] parameters = {
					new SqlParameter("@mobile", SqlDbType.NVarChar),
                    new SqlParameter("@code", SqlDbType.NVarChar),
                    new SqlParameter("@userActionSource", SqlDbType.Int,4)
			};
            parameters[0].Value = mobile;
            parameters[1].Value = code;
            parameters[2].Value = userActionSource;
            var ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            return ds.Tables[0].Rows.Count > 0 ? ds : null;
        }
		#endregion
	}
}
